<?php

namespace app\admin\model\pay;

use think\Model;


class Merchant extends Model
{

    

    // 表名
    protected $table = 'pay_merchant';
    // 设置当前模型的数据库连接
    protected $connection = 'db_zhongtai';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'type_text',
        'wechat_text',
        'no_password_text',
        'quota_time_text'
    ];
    
    const CACHE_TOKEN_KEY = 'pay_merchant8981';
    const STATUS_1 = '1';//上线
    
    public function getTypeList()
    {
        return ['1' => __('Type 1'), '2' => __('Type 2')];
    }

    public function getWechatList()
    {
        return ['1' => __('Wechat 1'), '2' => __('Wechat 2')];
    }

    public function getNoPasswordList()
    {
        return ['1' => __('No_password 1'), '2' => __('No_password 2')];
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getWechatTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['wechat']) ? $data['wechat'] : '');
        $list = $this->getWechatList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getNoPasswordTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['no_password']) ? $data['no_password'] : '');
        $list = $this->getNoPasswordList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getQuotaTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['quota_time']) ? $data['quota_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setQuotaTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }
    /**
     * [restoreQuota 恢复额度]
     * @since  2022-10-16
     * @return [type]     [description]
     */
    public function restoreQuota()
    {
        //恢复满额度
        return Merchant::where('id','=','0')->update(['today_quota'=>\think\Db::raw('quota')]);
        // return Merchant::where('id','<>','0')->update(['today_quota'=>\think\Db::raw('quota')]);
    }
    const WECHAT_1 = '1';//微信没有开通
    const WECHAT_2 = '2';// 微信开通
    const ALIPAY_1 = '1';// 支付宝没有开通
    const ALIPAY_2 = '2';// 支付宝开通
    public function getProvince($province,$total_fee,$wechat = false)
    {
        $xingwu = config('thirdpay.xingwu');
        $where = [
            'province'=>$province,// 省份
            'status'=>self::STATUS_1,//上线
            'c_mchid'=>$xingwu['mchid'],//配置的商户ID
        ];
        if ($total_fee <= 1) {
            // unset($where['province']);
        }
        if ($wechat) {
            $where['wechat'] = self::WECHAT_2;//是否需要开通微信
        }else{
            $where['alipay'] = self::ALIPAY_2;//需要开通支付宝
        }
        return $this->where($where)
            ->where('today_quota','>=',$total_fee)// 额度够用
            ->where('token','<>','')// 登录之后
            ->order('weight ASC')// 权重排序
            // ->fetchSql(true)// 权重排序
            ->find();
    }
    const IS_DEFAULT_1 = '1';// 是默认账号
    const IS_DEFAULT_2 = '2';// 不是默认账号
    public function getDefaultProvince($total_fee,$wechat = false)
    {
        $xingwu = config('thirdpay.xingwu');
        $where = [
            'is_default'=>self::IS_DEFAULT_1,// 默认账号
            'status'=>self::STATUS_1,//上线
            'c_mchid'=>$xingwu['mchid'],//配置的商户ID
        ];
        if ($wechat) {
            $where['wechat'] = self::WECHAT_2;//是否需要开通微信
        }else{
            $where['alipay'] = self::ALIPAY_2;//需要开通支付宝
        }
        return $this->where($where)
            ->where('today_quota','>=',$total_fee)// 额度够用
            ->where('token','<>','')// 登录之后
            ->order('weight ASC')// 权重排序
            // ->fetchSql(true)// 权重排序
            ->find();
    }
}
